package com.xxx.video.commen.utils;

import android.content.Context;
import android.content.SharedPreferences;


import com.xxx.video.VideoApplication;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.Set;

public class SPUtil {

    private static final String SP_NAME = VideoApplication.getInstance().getPackageName();
    private static SharedPreferences sp;

    public static void putlong(String key, long value) {
        if (sp == null) {
            sp = VideoApplication.getInstance().getSharedPreferences(SP_NAME, 0);
        }

        sp.edit().putLong(key, value).apply();
    }

    public static void putString(String key, String value) {
        if (sp == null) {
            sp = VideoApplication.getInstance().getSharedPreferences(SP_NAME, 0);
        }

        sp.edit().putString(key, value).apply();
    }

    public static void putInt(String key, int value) {
        if (sp == null) {
            sp = VideoApplication.getInstance().getSharedPreferences(SP_NAME, 0);
        }

        sp.edit().putInt(key, value).apply();
    }

    public static void putFloat(String key, float value) {
        if (sp == null) {
            sp = VideoApplication.getInstance().getSharedPreferences(SP_NAME, 0);
        }

        sp.edit().putFloat(key, value).apply();
    }

    public static void putStringSet(String key, Set value) {
        if (sp == null) {
            sp = VideoApplication.getInstance().getSharedPreferences(SP_NAME, 0);
        }

        sp.edit().putStringSet(key, value).apply();
    }

    public static void putBoolean(String key, boolean value) {
        if (sp == null) {
            sp = VideoApplication.getInstance().getSharedPreferences(SP_NAME, 0);
        }

        sp.edit().putBoolean(key, value).apply();
    }

    public static boolean getBoolean(String key, boolean value) {
        if (sp == null) {
            sp = VideoApplication.getInstance().getSharedPreferences(SP_NAME, 0);
        }

        return sp.getBoolean(key, value);
    }

    public static Set getStringSetn(String key, Set value) {
        if (sp == null) {
            sp = VideoApplication.getInstance().getSharedPreferences(SP_NAME, 0);
        }

        return sp.getStringSet(key, value);
    }

    public static int getInt(String key, int value) {
        if (sp == null) {
            sp = VideoApplication.getInstance().getSharedPreferences(SP_NAME, 0);
        }

        return sp.getInt(key, value);
    }

    public static String getString(String key, String value) {
        if (sp == null) {
            sp = VideoApplication.getInstance().getSharedPreferences(SP_NAME, 0);
        }

        return sp.getString(key, value);
    }

    public static long getLong(String key, long value) {
        if (sp == null) {
            sp = VideoApplication.getInstance().getSharedPreferences(SP_NAME, 0);
        }

        return sp.getLong(key, value);
    }

    public static float getFloat(String key, float value) {
        if (sp == null) {
            sp = VideoApplication.getInstance().getSharedPreferences(SP_NAME, 0);
        }

        return sp.getFloat(key, value);
    }

    public static void remove(String key) {
        SharedPreferences sp = VideoApplication.getInstance().getSharedPreferences(SP_NAME, 0);
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(key);
        SPUtil.SharedPreferencesCompat.apply(editor);
    }

    public static void clear() {
        SharedPreferences sp = VideoApplication.getInstance().getSharedPreferences(SP_NAME, 0);
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        SPUtil.SharedPreferencesCompat.apply(editor);
    }

    public static boolean contains(String key) {
        SharedPreferences sp = VideoApplication.getInstance().getSharedPreferences(SP_NAME, 0);
        return sp.contains(key);
    }

    public static Map<String, ?> getAll(Context context) {
        SharedPreferences sp = context.getSharedPreferences(SP_NAME, 0);
        return sp.getAll();
    }

    private static class SharedPreferencesCompat {
        private static final Method sApplyMethod = findApplyMethod();

        private SharedPreferencesCompat() {
        }

        private static Method findApplyMethod() {
            try {
                Class clz = SharedPreferences.Editor.class;
                return clz.getMethod("apply");
            } catch (Exception var1) {
                var1.printStackTrace();
                return null;
            }
        }

        public static void apply(SharedPreferences.Editor editor) {
            try {
                if (sApplyMethod != null) {
                    sApplyMethod.invoke(editor);
                    return;
                }
            } catch (Exception var4) {
                var4.printStackTrace();
            }

            editor.commit();
        }
    }
}
